<?php
/**
 * Provide shortcuts for lets manager configuration
 *
 * @package    letsManager
 * @subpackage misc
 * @author     Jerome Fouilloy
 * @version    SVN: $Rev$
 */
abstract class LetsConfiguration {

  const MONEY_ALARM_MAXI = "money_alarm_maxi";

  const MONEY_ALARM_MINI = "money_alarm_mini";

  const MONEY_NEW_MEMBER = "money_new_member";

  const MONEY_NAME = "money_name";

  const MEMBER_MAXI_ASKS_AND_OFFERS = "member_max_asksAndOffers";
  /**
   * Get alarm maximum for money of member
   * @param pwUser $user
   * @return integer the amount maximum for each member
   */
  public static function moneyAlarmMaxi($asso_id)
  {
    return intval(Configurator::get(self::MONEY_ALARM_MAXI,$asso_id));
  }

  public static function moneyAlarmMini($asso_id)
  {
    return Configurator::get(self::MONEY_ALARM_MINI,$asso_id);
  }

  public static function moneyNewMember($asso_id)
  {
    return Configurator::get(self::MONEY_NEW_MEMBER,$asso_id);
  }

  public static function memberUsernameForAssociation($asso_id)
  {
    return Configurator::get(self::ASSOCIATION_MEMBER_USERNAME,$asso_id,null);
  }
  
  public static function moneyName($asso_id)
  {
    return Configurator::get(self::MONEY_NAME,$asso_id);
  }
  /**
   * Number of days highlight ask before automatic delete (because expired)
   * @param integer $asso_id
   * @return integer number of days
   */
  public static function nbDaysHighlightBeforeDeleteAsk($asso_id)
  {
    return 6;
  }
  /**
   * Number of days between to send expiration email for ask and the removing action
   * (used only for exchange with confirmation before delete)
   * @param integer $asso_id
   * @return integer number of days
   */
  public static function nbDaysDeleteAskAfterMail($asso_id)
  {
    return 20;
  }
  public static function colorHighlightAskNearExpiration()
  {
    return "#FAC170";
  }
  
  /**
   * Get maximum of asks and offers by member
   * @param integer $asso_id
   */
  public static function nbMemberMaxiAsksAndOffers($asso_id)
  {
    return Configurator::get(self::MEMBER_MAXI_ASKS_AND_OFFERS,$asso_id);
  }
  
  /**
   * Get members recipients alarm money
   * @param integer $asso_id
   * @return array of Member
   */
  public static function membersForAlarmMoney($asso_id)
  {
    return MemberTable::getMembersForAclRight(LetsManagerRights::RECIPIENT_ALARM_MONEY);
  }
  
  /**
   * Get money account for association
   * @param integer $asso_id
   * @return LetsMoneyAccount the account money for association
   */
  public static function moneyAccountForAssociation($asso_id)
  {
    return LetsMoneyAccountTable::getAccountByAssociationId($asso_id);
  }
  
  /**
   * Get member id for current association, we use money account table to get member id
   * @param integer $asso_id
   * @return integer member id for association
   */
  public static function memberIdForAssociation($asso_id)
  {

    $memberId = null;
    // if call by night task we don't have context
    $currentUser = null;
    // PERF: cache the association member for all actions
    if(sfContext::getInstance() != null)
    {
      $currentUser = sfContext::getInstance()->getUser();
      $memberId = $currentUser->getAttribute('memberIdForAssociation',-1,'LetsConfiguration');
    }

    if($memberId == -1)
    {
      // only one money account for association
      $moneyAccount = LetsMoneyAccountTable::getAccountByAssociationId($asso_id);
      if($currentUser != null)
      {
        $currentUser->setAttribute('memberIdForAssociation',$moneyAccount->getMemberId(),'LetsConfiguration');
      }
    }
    return $memberId;
  }
  
  public static function redirectErrorConfig(sfAction $action, $variable, $problem)
  {
    $module = urlencode($action->getModuleName());
    $actionSource = urlencode($action->getActionName());
    $var = urlencode($variable);
    $action->redirect('@lets_error_config?moduleSource='.$module.'&actionSource='.$actionSource.'&variable='.$var.'&problem='.
    urlencode($problem));
  }
}